package com.tgy.clickhouse.aspect;

import com.tgy.clickhouse.annotation.DataSourceAnnotation;
import com.tgy.clickhouse.config.RoutingDataSourceContext;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;


/**
 * 设置切面
 * 使自己的注解生效，把指定的数据源交给spring
 */
@Aspect
@Component
public class DataSourceAspect {
    public DataSourceAspect() {
    }

    //表示在有这个注解的时候 执行
    @Before("@annotation(dataSourceAnnotation)")
    public void beforeSwitchDataSource(DataSourceAnnotation dataSourceAnnotation) {
        RoutingDataSourceContext.RoutingDataSourceContext(dataSourceAnnotation.value());
    }

    @After("@annotation(dataSourceAnnotation)")
    public void afterSwitchDataSource(DataSourceAnnotation dataSourceAnnotation) {
        RoutingDataSourceContext.close();
    }

}
